#include "Texture.h"

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

Texture::Texture(const std::string filePath, unsigned int textureIndex) {
	glGenTextures(1, &mTextureId);
	mTextureUnit = GL_TEXTURE0 + textureIndex;
	glActiveTexture(mTextureUnit);
	glBindTexture(GL_TEXTURE_2D, mTextureId);
	stbi_set_flip_vertically_on_load(true);
	unsigned char* imgData = stbi_load(filePath.c_str(), &mWidth, &mHeight, nullptr, STBI_rgb_alpha);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgData);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	stbi_image_free(imgData);
	glBindTexture(GL_TEXTURE_2D, 0);
}

void Texture::bindTexture() const {
	glActiveTexture(mTextureUnit);
	glBindTexture(GL_TEXTURE_2D, mTextureId);
}